package com.financing.wap.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.financing.wap.common.BizEnums;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dao.IUserInfoDao;
import com.financing.wap.dao.wherecondition.WC;
import com.financing.wap.dao.wherecondition.WhereCondition;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.dto.UserInfoDTO;
import com.financing.wap.service.IUserInfoService;

@Service("userInfoServiceImpl")
@Component("userInfoServiceImpl")
public class UserInfoServiceImpl extends BaseServiceImpl implements IUserInfoService{

	@Autowired
	private IUserInfoDao userInfoDaoImpl;

	@Override
	public List<UserInfoDTO> queryAllUserInfo(UserInfoDTO userInfoDTO) {
		return userInfoDaoImpl.queryAllUserInfo(userInfoDTO);
	}

	@Override
	public List<UserInfoDTO> queryOrgTree(WhereCondition wc) {
		return userInfoDaoImpl.queryOrgTree(wc);
	}

	/**
	 * 根据登录人获取组织下所有内部理财师telphone
	 */
	@Override
	public String queryPhoneLayerPath(String loginName, String orgid) {
		if(StringUtils.isNotBlank(loginName)) {//根节点查询
			UserInfoDTO userInfo =  new UserInfoDTO();
			userInfo.setTelePhone(loginName);
			userInfo.setDeptflag(BizEnums.DeptType.PERSON);//人员
			userInfo.setStatus(0);
			UserInfoDTO user = queryUserInfo(userInfo);//当前登录APP的理财师

			if(user == null) {//非机构理财师
				return loginName;
			} else {//机构理财师
				String sql = getQuerySql(user.getUserId());
				
				if(StringUtils.isBlank(sql)){
					return loginName;
				}

				WhereCondition wc = WC.one();
				wc.andCustom(sql);
				return userInfoDaoImpl.queryPhoneLayerPath(wc);
			}
		} else {//非根节点查询
			UserInfoDTO userInfo =  new UserInfoDTO();
			userInfo.setUserId(orgid);
			UserInfoDTO user = queryUserInfo(userInfo);

			//判断是机构人员还是机构
			if(StringUtils.equals(user.getDeptflag(), BizEnums.DeptType.DEPT)) {//机构
				String sql = " AND layerPath like '%"+user.getLayerPath()+"%'";//查询条件

				WhereCondition wc = WC.one();
				wc.andCustom(sql);
				return userInfoDaoImpl.queryPhoneLayerPath(wc);
			} else {//人员
				//人员直接返回
				return user.getTelePhone();
//				Session session = SessionUtil.getSession();
//				UserDTO loginUser = session.getUser();
				//判断选择的机构人员是否是登录用户
//				if(StringUtils.equals(loginUser.getLoginName(), user.getTelePhone())) {
//					//选择的机构人员是登录用户
//					String sql = getQuerySql(user.getUserId());
//
//					WhereCondition wc = WC.one();
//					wc.andCustom(sql);
//					return userInfoDaoImpl.queryPhoneLayerPath(wc);
//				} else {
//					//选择的机构人员非登录用户
//					return user.getTelePhone();
//				}
				
			}

		}

	}

	/**
	 * 根据登录人获取组织下所有内部理财师userid
	 */
	@Override
	public UserInfoDTO queryUserInfo(UserInfoDTO userInfoDTO) {
		return userInfoDaoImpl.queryUserInfo(userInfoDTO);
	}

	@Override
	public String queryUseridByLayerPath(String loginName, String orgid) {
		if(StringUtils.isNotBlank(loginName)) {//根节点查询
			UserInfoDTO userInfo =  new UserInfoDTO();
			userInfo.setTelePhone(loginName);
			userInfo.setDeptflag(BizEnums.DeptType.PERSON);//人员
//			userInfo.setLayerPath("3ea3a04e-4f70-11e8-b8ea-ecf4bbce3884");//只查销售部
			userInfo.setStatus(0);

			UserInfoDTO user = queryUserInfo(userInfo);//当前登录APP的理财师

			if(user == null) {//非机构理财师
				return null;
			} else {//机构理财师
				String sql = getQuerySql(user.getUserId());
				
				if(StringUtils.isBlank(sql)){
					return loginName;
				}

				WhereCondition wc = WC.one();
				wc.andCustom(sql);
				List<String> useridList = userInfoDaoImpl.queryUseridByLayerPath(wc);

				return StringUtils.join(useridList.toArray(), ",");
			}
		} else {//非根节点查询
			UserInfoDTO userInfo =  new UserInfoDTO();
			userInfo.setUserId(orgid);
//			userInfo.setLayerPath("3ea3a04e-4f70-11e8-b8ea-ecf4bbce3884");//只查销售部
			UserInfoDTO user = queryUserInfo(userInfo);

			//判断是机构人员还是机构
			if(StringUtils.equals(user.getDeptflag(), BizEnums.DeptType.DEPT)) {//机构
				String sql = " AND layerPath like '%"+user.getLayerPath()+"%'";//查询条件

				WhereCondition wc = WC.one();
				wc.andCustom(sql);
				List<String> useridList = userInfoDaoImpl.queryUseridByLayerPath(wc);

				return StringUtils.join(useridList.toArray(), ",");
			} else {//人员
				Session session = SessionUtil.getSession();
				UserDTO loginUser = session.getUser();

				//判断选择的机构人员是否是登录用户
				if(StringUtils.equals(loginUser.getLoginName(), user.getTelePhone())) {
					//选择的机构人员是登录用户
					String sql = getQuerySql(user.getUserId());

					WhereCondition wc = WC.one();
					wc.andCustom(sql);
					List<String> useridList = userInfoDaoImpl.queryUseridByLayerPath(wc);

					return StringUtils.join(useridList.toArray(), ",");
				} else {
					//选择的机构人员非登录用户
					return user.getUserId();
				}
			}

		}
	}

	/**
	 * 获取查询条件 <金策师机构版 - 机构人员查询>
	 * 
	 * @param userid
	 * @return
	 */
	private String getQuerySql(String userid) {
		UserInfoDTO userInfo =  new UserInfoDTO();
		userInfo.setDeptHead(userid);
		userInfo.setDeptflag(BizEnums.DeptType.DEPT);//部门
		userInfo.setStatus(0);
		userInfo.setLayerPath("3ea3a04e-4f70-11e8-b8ea-ecf4bbce3884");//只查销售部
		List<UserInfoDTO> deptlist = queryAllUserInfo(userInfo); //登录人员负责的下属部门

		String sql = "";//查询条件
		int i=0;
		if(deptlist.size()==1) {
			sql = " AND layerPath like '%"+deptlist.get(0).getLayerPath()+"%'";
		} else {
			for(UserInfoDTO u:deptlist) {
				if(i==0) {
					sql += " AND (layerPath like '%"+u.getLayerPath()+"%'";
				} else if(i==deptlist.size()-1){
					sql += " OR layerPath like '%"+u.getLayerPath()+"%')";
				} else {
					sql += " OR layerPath like '%"+u.getLayerPath()+"%'";
				}
				i++;
			}
		}

		return sql;
	}

}
